package com.example.dashabi

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val viewModel: MainModel by viewModels()
        setContent {
            Scaffold(
                topBar = {
                    AppBar(context = this,viewModel=viewModel, titleS = viewModel.titles[viewModel.currentPage], modifier = Modifier)
                },
                bottomBar = {
                    HomeBottomBar(viewModel.currentPage) {
                        viewModel.currentPage = it
                        viewModel.stringS = it.toString()
                    }
                }

            ) {
                LazyColumn(
                  Modifier.padding(bottom = 48.dp)
                ) {
                    items(viewModel.items.size) { index ->
                        learnConstLayout(viewModel.items[index])
                        Divider(modifier = Modifier
                            .background(color = Color.Gray)
                            .height(1.dp))
                    }
                }
            }
        }
    }
}

@Composable
fun AppBar(context: Context? = null, viewModel: MainModel,titleS: String, modifier: Modifier) {
    ConstraintLayout(
        modifier = modifier
            .height(48.dp)
            .background(color = Color.White)
            .fillMaxWidth()

    ) {
        val (title, leading, action,dividerLine) = createRefs()
        Text(text = titleS, modifier = Modifier.constrainAs(title) {
            top.linkTo(parent.top)
            bottom.linkTo(parent.bottom)
            start.linkTo(parent.start)
            end.linkTo(parent.end)

        }, color = Color.Black)
        Image(
            painter = painterResource(id = R.drawable.logof278d355),
            contentDescription = "",
            modifier = Modifier
                .constrainAs(leading) {//通过constrainAs将Image与imageRef绑定,并增加约束
                    top.linkTo(parent.top)
                    start.linkTo(parent.start, 16.dp)
                    bottom.linkTo(parent.bottom)
                },
            contentScale = ContentScale.Fit)

        ActionButton(modifier = Modifier
            .constrainAs(action) {
                top.linkTo(title.top)
                bottom.linkTo(title.bottom)
                end.linkTo(parent.end, 16.dp)
            }
            .clickable {
//                (context as AppCompatActivity).finish()
//                viewModel.items.add(ItemEntity().apply { name="666" })
                viewModel.items.add(ItemEntity().apply { name = "666" })
                Log.e("viewModel", viewModel.items.size.toString());
//
            })
        Divider(
            Modifier
                .constrainAs(dividerLine) {
                    bottom.linkTo(parent.bottom)

                }
                .height(1.dp)
                .background(Color.Gray))
    }

}

@Composable
fun ActionButton(
    modifier: Modifier
) {
    ConstraintLayout(
        modifier = modifier
            .wrapContentWidth()
            .height(35.dp)
            .background(color = Color.White)
    ) {
        val (icon, content, my_view) = createRefs()
        Image(
            painter = painterResource(id = R.drawable.ic_launcher),
//            vectorResource(R.drawable.ic_back),
            contentDescription = null,
            Modifier
                .constrainAs(icon) {
                    top.linkTo(parent.top)
                    start.linkTo(parent.start, 4.dp)
                    bottom.linkTo(parent.bottom)
                }
                .size(30.dp),
        )
        Text(text = "登录", modifier = Modifier.constrainAs(content) {
            top.linkTo(parent.top)
            bottom.linkTo(parent.bottom)
            start.linkTo(icon.end)
        }, style = TextStyle(color = Color.Gray, fontSize = 12.sp))

        AndroidView(
            modifier = Modifier.constrainAs(my_view) {
                top.linkTo(parent.top)
                bottom.linkTo(parent.bottom)
                start.linkTo(icon.start)
            }, // Occupy the max size in the Compose UI tree
            factory = { context ->
                // Creates custom view
                EditText(context).apply {
                    // Sets up listeners for View -> Compose communication
                    this.setText("大傻逼")
                }
            },
//            update = { view ->
//                // View's been inflated or state read in this block has been updated
//                // Add logic here if necessary
//
//                // As selectedItem is read here, AndroidView will recompose
//                // whenever the state changes
//                // Example of Compose -> View communication
//                view.coordinator.selectedItem = selectedItem.value
//            }
            update = {
                var s = it.text.toString()
                Log.e("大傻逼",s)
            }
        )
    }
}


@Composable
fun learnConstLayout(entity:ItemEntity) {

    ConstraintLayout(
        Modifier
            .height(150.dp)
            .fillMaxWidth()
            .background(color = Color.White)
    ) {
        val (imageRef, nameRef, descRef, descRef2, pass) = createRefs()
        Image(painter = painterResource(id = R.drawable.ic_launcher),
            contentDescription = "dog avatar",
            modifier = Modifier
                .constrainAs(imageRef) {//通过constrainAs将Image与imageRef绑定,并增加约束
                    top.linkTo(pass.bottom, 16.dp)
                    start.linkTo(pass.start)
                    bottom.linkTo(parent.bottom, 16.dp)
                }
                .size(50.dp)
                .clip(shape = RoundedCornerShape(5)),
            contentScale = ContentScale.Crop)

        Text(
            text = entity.name!!, modifier = Modifier
                .constrainAs(pass) {
                    top.linkTo(parent.top, 8.dp)
                    start.linkTo(parent.start, 8.dp)

                }
                .wrapContentSize(),
            style = TextStyle(color = Color.Gray, fontSize = 30.sp)
        )
        Text(
            text = "名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称名称",
            modifier = Modifier
                .constrainAs(nameRef) {
                    top.linkTo(imageRef.top)
                    start.linkTo(imageRef.end, 8.dp)
                    end.linkTo(parent.end, 8.dp)
                    width = Dimension.fillToConstraints
                }
                .fillMaxWidth(),
            fontSize = 18.sp,
            maxLines = 1,
            textAlign = TextAlign.Left,
            overflow = TextOverflow.Ellipsis,
        )

        Text(
            text = "描述描述描述", modifier = Modifier
                .constrainAs(descRef) {
                    start.linkTo(nameRef.start)
                    bottom.linkTo(imageRef.bottom)
                    width = Dimension.wrapContent
                },
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            style = TextStyle(color = Color.Blue, fontSize = 13.sp)
        )

        Text(
            text = "另一个描述",
            modifier = Modifier
                .constrainAs(descRef2) {
                    start.linkTo(descRef.end, 5.dp)
                    top.linkTo(descRef.top)
                    bottom.linkTo(descRef.bottom)
                    end.linkTo(nameRef.end)
                    width = Dimension.fillToConstraints

                },
            maxLines = 1,
            textAlign = TextAlign.Start,
            overflow = TextOverflow.Ellipsis,
            style = TextStyle(color = Color.Red, fontSize = 13.sp)
        )
    }

}

@Preview()
@Composable
fun AppBarPreview() {
//    AppBar(titleS = "标题", modifier = Modifier)
}

@Preview(showBackground = true)
@Composable
fun HomeViewPreview() {
//    learnConstLayout()

}